package me.zhengjie.modules.finace_taxation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import me.zhengjie.base.PageInfo;
import me.zhengjie.base.QueryHelpMybatisPlus;
import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.modules.finace_taxation.domain.FtIndustryDetails;
import me.zhengjie.modules.finace_taxation.service.FtIndustryDetailsService;
import me.zhengjie.modules.finace_taxation.service.dto.FtIndustryDetailsDto;
import me.zhengjie.modules.finace_taxation.service.dto.FtIndustryDetailsQueryParam;
import me.zhengjie.modules.finace_taxation.service.mapper.FtIndustryDetailsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
* @author 杨荣森
* @date 2021-06-23
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = FtIndustryDetailsService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FtIndustryDetailsServiceImpl implements FtIndustryDetailsService {

    // private final RedisUtils redisUtils;
    private final FtIndustryDetailsMapper ftIndustryDetailsMapper;

    @Override
    public PageInfo<FtIndustryDetailsDto> queryAll(FtIndustryDetailsQueryParam query, Pageable pageable) {
        QueryWrapper<FtIndustryDetails> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(FtIndustryDetails::getTitle, query.getTitle());
        IPage<FtIndustryDetails> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FtIndustryDetails> page = ftIndustryDetailsMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, FtIndustryDetailsDto.class);
    }

    @Override
    public List<FtIndustryDetailsDto> queryAll(FtIndustryDetailsQueryParam query){
        return ConvertUtil.convertList(ftIndustryDetailsMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), FtIndustryDetailsDto.class);
    }

    @Override
    public FtIndustryDetails getById(String id) {
        return ftIndustryDetailsMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public FtIndustryDetailsDto findById(String id) {
        return ConvertUtil.convert(getById(id), FtIndustryDetailsDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(FtIndustryDetailsDto resources) {
        FtIndustryDetails entity = ConvertUtil.convert(resources, FtIndustryDetails.class);
        return ftIndustryDetailsMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(FtIndustryDetailsDto resources){
        FtIndustryDetails entity = ConvertUtil.convert(resources, FtIndustryDetails.class);
        int ret = ftIndustryDetailsMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return ftIndustryDetailsMapper.deleteBatchIds(ids);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    @Override
    public List<FtIndustryDetails> queryIndustryList() {
        return ftIndustryDetailsMapper.selectList(null);
    }

    @Override
    public PageInfo<FtIndustryDetails> getIndustryByPage(Map<String, Object> map) {
        int pageNo = Integer.parseInt(map.get("pageNo").toString());
        int pageSize = Integer.parseInt(map.get("pageSize").toString());
        Page<FtIndustryDetails> page = new Page<>(pageNo, pageSize);
        IPage<FtIndustryDetails> pageInfo = ftIndustryDetailsMapper.getIndustryByPage(page, map);
        return ConvertUtil.convertPage(pageInfo, FtIndustryDetails.class);
    }

    @Override
    public int addBrowseNumber(String id) {
        return ftIndustryDetailsMapper.addBrowseNumber(id);
    }

    /*
    private void delCaches(String id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<String> ids) {
        for (String id: ids) {
            delCaches(id);
        }
    }*/

    @Override
    public void download(List<FtIndustryDetailsDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (FtIndustryDetailsDto ftIndustryDetails : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("行业详情标题", ftIndustryDetails.getTitle());
              map.put("行业详情封面",ftIndustryDetails.getImgUrl());
              map.put("行业详情描述", ftIndustryDetails.getDescript());
              map.put("发布时间", ftIndustryDetails.getReleaseTime());
              map.put("浏览次数", ftIndustryDetails.getBrowseNumber());
              map.put("是否发布：1.是，0.否", ftIndustryDetails.getPutShelf());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }
}
